var filterMessage = 'filterMessage';

function backToParent(){
	$('#reportListDiv').show();
	$('#reportContentDiv').html('').hide();	
}

/**
 * Display report filter and report button
 * @param reportName
 */
function displayReport(reportName) {		
	$.ajax({
		type:	'POST',
		url:	'../ReportManager/handle?set_ajax_view',
		data:	'reportName=' + reportName +
				'&actionName=Display',
		dataType: 'json',
		beforeSend: function() {$('#ajaxLoader').show();},
		complete: function() {$('#ajaxLoader').hide();},
		success: function(data) {
			if (data.error.code == 'OK') {
				$('#reportListDiv').hide();
				$('#reportContentDiv').html(data.result).show();				
			} else {
				showErrorDialog(data.error.message);					
			}
			
		}
	});
}

/**
 * View report as HTML
 * @param reportName
 */
function viewReport(reportName) {
	if (!validateReport()) return false;	
	var filter = getReportFilterData();		
	
	$.ajax({
		type:	'POST',
		url:	'../ReportManager/handle?set_ajax_view',
		data:	filter +  
				'&reportName=' + reportName +
				'&actionName=View',
		dataType: 'json',
		beforeSend: function() {$('#ajaxLoader').show();},
		complete: function() {$('#ajaxLoader').hide();},
		success: function(data) {
			if (data.error.code == 'OK') {				
				$('#reportBody').html(data.result);				
			} else {				
				showErrorDialog(data.error.message);	
			}
			
		}
	});
}

/**
 * Print Report
 * @param reportName
 */
function printReport(reportName) {
	if (!validateReport()) return false;	
	var filter = getReportFilterData();	
		
	var url = '../' + reportName + '/Print?set_print_view&' + filter;	
	window.open(url, '', 'scrollbars=yes, menubar=no, resizable=yes, toolbar=no, location=no, status=no');
}

/**
 * Export report to specified file format
 * @param reportName
 */
function exportReport(reportName) {
	if (!validateReport()) return false;
	alert('pls write this function body');
}

////////// Validation and get data for SINGLE DATE Filter ////////////

function validateSingleDate() {
	return checkDate($('#start_yyyy'), $('#start_mm'), $('#start_dd'), true, _('Invalid date'), filterMessage, false);	 
}

function getDataSingleDate() {
	var startDay = $.trim($('#start_dd').val());
    var startMonth = $.trim($('#start_mm').val());
    var startYear = $.trim($('#start_yyyy').val()); 
    var startDate   =   startYear + "-" + startMonth + "-" + startDay;
    
    return "startDate=" + startDate + "&endDate=" + startDate;        
}

////////// Validation and get data for FULL DATE Filter ////////////

function validateFullDate() {
	alert('This function is under implementation');
}

function getDataFullDate() {
	alert('This function is under implementation');
}

////////// Validation and get data for FROM-TO DATE Filter ////////////

function validateFromToDate() {
	var checkStartDate = checkDate($('#start_yyyy'), $('#start_mm'), $('#start_dd'), true, _('Invalid date'), filterMessage, false);	
	if (checkStartDate == false) return false;
	
	var checkEndDate = checkDate($('#end_yyyy'), $('#end_mm'), $('#end_dd'), true, _('Invalid date'), filterMessage, false);	
	return checkEndDate;
	
}

function getDataFromToDate() {
	var startDay = $.trim($('#start_dd').val());
    var startMonth = $.trim($('#start_mm').val());
    var startYear = $.trim($('#start_yyyy').val()); 
    var startDate   =   startYear + "-" + startMonth + "-" + startDay;
    
    var endDay = $.trim($('#end_dd').val());
    var endMonth = $.trim($('#end_mm').val());
    var endYear = $.trim($('#end_yyyy').val()); 
    var endDate   =   endYear + "-" + endMonth + "-" + endDay;
    
    return "startDate=" + startDate + "&endDate=" + endDate;
}

/**
 * Show error dialog
 * @param message
 */
function showErrorDialog(message) {
	var buttons = {};
	buttons[_('Close')] = function() {
		$(this).dialog('close');					
	};	
	generalDialog(null, _('Error'), message, 'error', {					
		resizable: false,				
		width: 400,
		height: 200,
		modal: true,
		buttons: buttons,
		dialogClass: 'custom-dialog'
	}, _('Close'));
}